<template>
  <dialogPublic
    :title="title+'-项目大事记'"
    :show.sync="showDialog"
    @doSubmit="updateMemorabilia"
    @doClose="doClose"
    :noFootBtn="noBtn"
    width="600px"
    height="400px"
    class="memorabiliaDialog"
  >
    <editForm :formConfig="memorabiliaConfig" ref="memorabilia" :formData="memorabiliaData" />
    <div class="attachConten">
      <div class="attachTitle">附件:</div>
      <div class="tebleDiv">
        <attachFormNew
          style="width: 60%"
          :disabled="this.showType == 'detail'"
          :attachSetting="attachSetting"
          isDelete
          multiple
          ref="attachForm"
        ></attachFormNew>
      </div>
    </div>
  </dialogPublic>
</template>
<script>
import {
  getMemorabiliaDetailsApi, //获取详情
  updateMemorabiliaApi
} from "@/api/specialManagement/specialParameter";
export default {
  props: {
    showDetails: {
      type: Boolean,
      default: true
    },
    noFootBtn: {
      type: Boolean,
      default: false
    },
    rowData: {
      type: Object,
      default: () => {
        return {};
      }
    },
    projectData: {
      type: Object,
      default: () => {
        return {};
      }
    },
    showType: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      showDialog: true,
      title: "",
      memorabiliaData: {},
      //附件设置
      attachSetting: {
        businessId: "", //单据的ID
        businessType: ""
      },
      memorabiliaConfig: {
        itemArr: [
          {
            label: "标题",
            prop: "title",
            type: "text",
            required: true,
            span: 24
          },
          {
            label: "内容",
            prop: "content",
            type: "textarea",
            maxlength: "1000",
            required: true,
            span: 24,
            rows: 6
          }
        ]
      }
    };
  },
  computed: {
    noBtn() {
      return this.showType == "detail";
    }
  },
  created() {
    this.init();
  },
  mounted() {},
  methods: {
    init() {
      this.attachSetting.businessType = this.projectData.id;
      if (this.showType != "add") {
        this.getDetails();
      }
      if (this.showType == "add") {
        this.title = "新增";
      } else if (this.showType == "edit") {
        this.title = "编辑";
      } else if (this.showType == "detail") {
        this.title = "详情";
        this.memorabiliaConfig.itemArr.map(i => {
          i.disabled = true;
        });
      }
    },
    getDetails() {
      getMemorabiliaDetailsApi(this.rowData.id).then(res => {
        let data = res.data || {};
        this.memorabiliaData = data;
        this.attachSetting.businessId = data.id || "";
      });
    },
    async updateMemorabilia() {
      let data = await this.$refs.memorabilia.getFormData();
      data.projectId = this.rowData.projectId;
      updateMemorabiliaApi(data).then(res => {
        if (this.showType == "add") {
          this.attachSetting.businessId = res.data.id;
        }
        this.saveAttachFile();
        this.$message.success("操作成功");
        this.$emit("updateData");
        this.doClose();
      });
    },
    doClose() {
      this.$emit("update:showDetails", false);
    },
    //保存附件
    saveAttachFile() {
      this.$refs.attachForm.save();
    }
  }
};
</script>
<style lang="less" scoped>
.memorabiliaDialog {
  /deep/.el-form-item__label {
    width: 60px !important;
  }
  /deep/.el-form-item__content {
    margin-left: 60px !important;
  }
  .attachConten {
    display: flex;
    flex-flow: wrap nowrap;
    .redPoint {
      color: red;
    }
    .attachTitle {
      width: 120px;
    }
    .tebleDiv {
      width: 100%;
    }
  }
}
</style>